package com.qfedu.ioc;

import com.qfedu.ioc.framework.annotation.Component;

import java.io.File;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class Test2 {
    private final static HashMap<Class, List<Class>> fatherSonHashMap=new HashMap<>();

    public static void main(String[] args) throws ClassNotFoundException{
        String basePackage="com.qfedu.ioc";
        URL resource = Test2.class.getResource("/");
        System.out.println(resource.getPath());
        String resourcePath = resource.getPath();
        String replaceAll = basePackage.replaceAll("\\.", "/");
       String path= resourcePath+replaceAll;

        File file = new File(path);
        File[] files = file.listFiles();
        for (File file1 : files) {
            String name = file1.getName();
            if (file1.isFile()&&name.endsWith(".class")){
                String[] split = name.split("\\.");
                Class<?> aClass = Class.forName(basePackage + "." + split[0]);
                Component annotation = aClass.getAnnotation(Component.class);
                if (annotation!=null){
                    Class<?>[] interfaces = aClass.getInterfaces();
                    for (int i = 0; i <interfaces.length ; i++) {
                        Class<?> anInterface=interfaces[i];

                        List<Class> sonList = fatherSonHashMap.get(anInterface);
                            if (sonList==null){
                                ArrayList<Class> sonClassList = new ArrayList<>();
                                sonClassList.add(aClass);
                                fatherSonHashMap.put(anInterface,sonClassList);
                            }else {
                                sonList.add(aClass);
                            }
                    }
                }
            }
        }

    }





}
